package com.simon.mctm.biz.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Role;
import com.simon.mctm.biz.sys.bean.User;
import com.simon.mctm.biz.sys.dao.RoleMapper;
import com.simon.mctm.biz.sys.service.RoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统角色 服务实现类
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 根据条件获取总条目数和总页数
     *
     * @param name     用户名
     * @param pageSize 单页条目数
     * @return 页数和总条目数对象
     */
    @Override
    public PageResult calPage(String name, Integer pageSize) {
        try {
            List<Role> roleList = roleMapper.selectList(
                    new EntityWrapper<Role>().like("name", name)
            );
            if (CollectionUtils.isNotEmpty(roleList)) {
                return new PageResult(roleList.size(),Utils.calPages(roleList.size(), pageSize));
            } else {
                return new PageResult(0,0);
            }
        } catch (Exception ex) {
            throw new OperationFailedException(ex.getMessage());
        }
    }
}
